home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / XML / Beautifier.php next >
PHP Script  |  2004-03-24  |  9KB  |  340 lines

  1. <?PHP
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stephan Schmidt <schst@php.net>                             |
  17. // +----------------------------------------------------------------------+
  18.  
  19. if( !defined( 'XML_BEAUTIFIER_INCLUDE_PATH' ) ) {
  20.     define( 'XML_BEAUTIFIER_INCLUDE_PATH', 'XML/Beautifier' );
  21. }
  22.  
  23. /**
  24.  * XML/Beautifier.php
  25.  *
  26.  * Package that formats your XML files, that means
  27.  * it is able to add line breaks, and indents your tags.
  28.  *
  29.  * @category XML
  30.  * @package  XML_Beautifier
  31.  * @author   Stephan Schmidt <schst@php.net>
  32.  */
  33.  
  34. /**
  35.  * element is empty
  36.  */
  37. define('XML_BEAUTIFIER_EMPTY', 0);
  38.  
  39. /**
  40.  * CData
  41.  */
  42. define('XML_BEAUTIFIER_CDATA', 1);
  43.  
  44. /**
  45.  * XML element
  46.  */
  47. define('XML_BEAUTIFIER_ELEMENT', 2);
  48.  
  49. /**
  50.  * processing instruction
  51.  */
  52. define('XML_BEAUTIFIER_PI', 4);
  53.  
  54. /**
  55.  * entity
  56.  */
  57. define('XML_BEAUTIFIER_ENTITY', 8);
  58.  
  59. /**
  60.  * comment
  61.  */
  62. define('XML_BEAUTIFIER_COMMENT', 16);
  63.  
  64. /**
  65.  * XML declaration
  66.  */
  67. define('XML_BEAUTIFIER_XML_DECLARATION', 32);
  68.  
  69. /**
  70.  * doctype declaration
  71.  */
  72. define('XML_BEAUTIFIER_DT_DECLARATION', 64);
  73.  
  74. /**
  75.  * default
  76.  */
  77. define('XML_BEAUTIFIER_DEFAULT', 128);
  78.  
  79. /**
  80.  * overwrite the original file
  81.  */
  82. define('XML_BEAUTIFIER_OVERWRITE', -1);
  83.  
  84. /**
  85.  * could not write to output file
  86.  */
  87. define('XML_BEAUTIFIER_ERROR_NO_OUTPUT_FILE', 151);
  88.  
  89. /**
  90.  * could not load renderer
  91.  */
  92. define('XML_BEAUTIFIER_ERROR_UNKNOWN_RENDERER', 152);
  93.  
  94. /**
  95.  * XML_Beautifier is a class that adds linebreaks and
  96.  * indentation to your XML files. It can be used on XML
  97.  * that looks ugly (e.g. any generated XML) to transform it 
  98.  * to a nicely looking XML that can be read by humans.
  99.  *
  100.  * It removes unnecessary whitespace and adds indentation
  101.  * depending on the nesting level.
  102.  *
  103.  * It is able to treat tags, data, processing instructions
  104.  * comments, external entities and the XML prologue.
  105.  *
  106.  * XML_Beautifier is using XML_Beautifier_Tokenizer to parse an XML
  107.  * document with a SAX based parser and builds tokens of tags, comments,
  108.  * entities, data, etc.
  109.  * These tokens will be serialized and indented by a renderer 
  110.  * with your indent string.
  111.  *
  112.  * Example 1: Formatting a file
  113.  * <code>
  114.  * require_once 'XML/Beautifier.php';
  115.  * $fmt = new XML_Beautifier();
  116.  * $result = $fmt->formatFile('oldFile.xml', 'newFile.xml');
  117.  * </code>
  118.  *
  119.  * Example 2: Formatting a string
  120.  * <code>
  121.  * require_once 'XML/Beautifier.php';
  122.  * $xml = '<root><foo   bar = "pear"/></root>';
  123.  * $fmt = new XML_Beautifier();
  124.  * $result = $fmt->formatString($xml);
  125.  * </code>
  126.  *
  127.  * @category XML
  128.  * @package  XML_Beautifier
  129.  * @version  1.0
  130.  * @author   Stephan Schmidt <schst@php.net>
  131.  */
  132. class XML_Beautifier {
  133.  
  134.    /**
  135.     * default options for the output format
  136.     * @var    array
  137.     * @access private
  138.     */
  139.     var $_defaultOptions = array(
  140.                          "removeLineBreaks"  => true,
  141.                          "removeLeadingSpace"=> true,       // not implemented, yet
  142.                          "indent"            => "    ",
  143.                          "linebreak"         => "\n",
  144.                          "caseFolding"       => false,
  145.                          "caseFoldingTo"     => "uppercase",
  146.                          "normalizeComments" => false,
  147.                          "maxCommentLine"    => -1,
  148.                          "multilineTags"     => false
  149.                         );
  150.  
  151.    /**
  152.     * options for the output format
  153.     * @var    array
  154.     * @access private
  155.     */
  156.     var $_options = array();
  157.     
  158.    /**
  159.     * Constructor
  160.     *
  161.     * This is only used to specify the options of the
  162.     * beautifying process.
  163.     *
  164.     * @access public
  165.     * @param  array  $options   options that override default options
  166.     */   
  167.     function XML_Beautifier($options = array())
  168.     {
  169.         $this->_options = array_merge($this->_defaultOptions, $options);
  170.         $this->folding = false;
  171.     }
  172.  
  173.    /**
  174.     * reset all options to default options
  175.     *
  176.     * @access   public
  177.     * @see      setOption(), XML_Beautifier(), setOptions()
  178.     */
  179.     function resetOptions()
  180.     {
  181.         $this->_options = $this->_defaultOptions;
  182.     }
  183.  
  184.    /**
  185.     * set an option
  186.     *
  187.     * You can use this method if you do not want to set all options in the constructor
  188.     *
  189.     * @access   public
  190.     * @see      resetOptions(), XML_Beautifier(), setOptions()
  191.     */
  192.     function setOption($name, $value)
  193.     {
  194.         $this->_options[$name] = $value;
  195.     }
  196.     
  197.    /**
  198.     * set several options at once
  199.     *
  200.     * You can use this method if you do not want to set all options in the constructor
  201.     *
  202.     * @access   public
  203.     * @see      resetOptions(), XML_Beautifier()
  204.     */
  205.     function setOptions($options)
  206.     {
  207.         $this->_options = array_merge($this->_options, $options);
  208.     }
  209.  
  210.    /**
  211.     * format a file or URL
  212.     *
  213.     * @access public
  214.     * @param  string    $file       filename
  215.     * @param  mixed     $newFile    filename for beautified XML file (if none is given, the XML string will be returned.)
  216.     *                               if you want overwrite the original file, use XML_BEAUTIFIER_OVERWRITE
  217.     * @param  string    $renderer   Renderer to use, default is the plain xml renderer
  218.     * @return mixed                 XML string of no file should be written, true if file could be written
  219.     * @throws PEAR_Error
  220.     * @uses   _loadRenderer() to load the desired renderer
  221.     */   
  222.     function formatFile($file, $newFile = null, $renderer = "Plain")
  223.     {
  224.         if ($newFile == XML_BEAUTIFIER_OVERWRITE) {
  225.             $newFile = $file;
  226.         }
  227.  
  228.         /**
  229.          * Split the document into tokens
  230.          * using the XML_Tokenizer
  231.          */
  232.         require_once XML_BEAUTIFIER_INCLUDE_PATH . '/Tokenizer.php';
  233.         $tokenizer = new XML_Beautifier_Tokenizer();
  234.         
  235.         $tokens = $tokenizer->tokenize( $file, true );
  236.  
  237.         if (PEAR::isError($tokens)) {
  238.             return $tokens;
  239.         }
  240.         
  241.         $renderer = $this->_loadRenderer($renderer, $this->_options);
  242.  
  243.         if (PEAR::isError($renderer)) {
  244.             return $renderer;
  245.         }
  246.         
  247.         $xml = $renderer->serialize($tokens);
  248.         
  249.         if ($newFile == null) {
  250.             return $xml;
  251.         }
  252.         
  253.         $fp = @fopen($newFile, "w");
  254.         if (!$fp) {
  255.             return PEAR::raiseError("Could not write to output file", XML_BEAUTIFIER_ERROR_NO_OUTPUT_FILE);
  256.         }
  257.         
  258.         flock($fp, LOCK_EX);
  259.         fwrite($fp, $xml);
  260.         flock($fp, LOCK_UN);
  261.         fclose($fp);
  262.         return true;
  263.     }
  264.  
  265.    /**
  266.     * format an XML string
  267.     *
  268.     * @access public
  269.     * @param  string    $string     XML
  270.     * @return string    formatted XML string
  271.     * @throws PEAR_Error
  272.     */   
  273.     function formatString($string, $renderer = "Plain")
  274.     {
  275.         /**
  276.          * Split the document into tokens
  277.          * using the XML_Tokenizer
  278.          */
  279.         require_once XML_BEAUTIFIER_INCLUDE_PATH . '/Tokenizer.php';
  280.         $tokenizer = new XML_Beautifier_Tokenizer();
  281.         
  282.         $tokens = $tokenizer->tokenize( $string, false );
  283.  
  284.         if (PEAR::isError($tokens)) {
  285.             return $tokens;
  286.         }
  287.  
  288.         $renderer = $this->_loadRenderer($renderer, $this->_options);
  289.  
  290.         if (PEAR::isError($renderer)) {
  291.             return $renderer;
  292.         }
  293.         
  294.         $xml = $renderer->serialize($tokens);
  295.         
  296.         return $xml;
  297.     }
  298.  
  299.    /**
  300.     * load a renderer
  301.     *
  302.     * Renderers are used to serialize the XML tokens back 
  303.     * to an XML string.
  304.     *
  305.     * Renderers are located in the XML/Beautifier/Renderer directory.
  306.     *
  307.     * @access   private
  308.     * @param    string  $renderer   name of the renderer
  309.     * @param    array   $options    options for the renderer
  310.     * @return   object              renderer
  311.     * @throws   PEAR_Error
  312.     */
  313.     function &_loadRenderer($name, $options = array())
  314.     {
  315.         $file = XML_BEAUTIFIER_INCLUDE_PATH . "/Renderer/$name.php";
  316.         $class = "XML_Beautifier_Renderer_$name";
  317.  
  318.         @include_once $file;
  319.         if (!class_exists($class)) {
  320.             return PEAR::raiseError( "Could not load renderer.", XML_BEAUTIFIER_ERROR_UNKNOWN_RENDERER );
  321.         }
  322.  
  323.         $renderer = &new $class($options);
  324.         
  325.         return $renderer;        
  326.     }
  327.     
  328.    /**
  329.     * return API version
  330.     *
  331.     * @access   public
  332.     * @static
  333.     * @return   string  $version API version
  334.     */
  335.     function apiVersion()
  336.     {
  337.         return "1.0";
  338.     }
  339. }
  340. ?>